package cn.com.idmy.cloud.util;


import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.validation.ValidationUtil;
import org.jetbrains.annotations.NotNull;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ValidUtil {

    public static void validate(@NotNull Object bean, @NotNull Class<?>... groups) {
        var set = ValidationUtil.validate(bean, groups);
        for (var obj : set) {
            if (StrUtil.startWith(obj.getMessageTemplate(), "{jakarta.validation.constraints")) {
                throw new IllegalArgumentException(obj.getPropertyPath() + ":" + obj.getMessage());
            } else {
                throw new IllegalArgumentException(obj.getMessage());
            }
        }
    }
}
